package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.ycm.common.utils.HexUtils;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2024-06-27 19:43:00
 */
public class Data2 extends Data {

    private String DeviceName;

    @JSONField(
            name = "DeviceName"
    )
    public String getDeviceName(JSONObject jsonObject) {
        String deviceName = jsonObject.getString("DeviceName");
        if (deviceName.length() > 16) {
            deviceName = deviceName.substring(0, 16);
        }
        if (deviceName.length() < 4) {
            deviceName += "0000";
        }
        return HexUtils.string2HexUTF8(deviceName);
    }

    public String getDataID() {
        // data-02
        return HexUtils.intToHex(2, 2);
    }


    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append(this.getDeviceName(jsonObject));

        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }
}
